﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace ik.DB.SQLServer
{
    public class clDbSQL : clDB
    {
        #region "ToString"
        /// <summary>
        /// class information
        /// </summary>
        /// <returns>String</returns>
        public override String ToString()
        {
            return "Class name: ik.DB.SQLServer \n\n" +
                   "Class version: " + Environment.Version.ToString() + "\n" +
                   "OS version: " + Environment.OSVersion.ToString() + "\n" +
                   "Computer name: " + Environment.MachineName + "\n" +
                   "Rights: (c) 2011 - by Ivan Kruger";
        }
        #endregion

        /// <summary>
        /// contrutor
        /// </summary>
        public clDbSQL()
            : base(enSGBD.SQLServer)
        {
        }

        #region "GetCnn"
        public void GetCnn(String cnn)
        {
            try
            {
                cnnString = cnn;
                //if (cnnDB == null)
                    cnnDB = new SqlConnection(cnnString);
                //if (cnnDB.State != ConnectionState.Open)
                    cnnDB.Open();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// conecta ao banco de dados SQLServer
        /// </summary>
        /// <param name="user">nome do usuario</param>
        /// <param name="pwd">senha do usuario encriptada</param>
        /// <param name="dataSource">instancia. ex: note03\x64</param>
        /// <param name="catalog">catalog - ex: Moto</param>
        /// <param name="partner">instancia de partner. ex: note04\x86</param>
        /// <param name="cnnTimeOut">tempo de conexão</param>
        public void GetCnn(String user, String pwd, String dataSource, String catalog, String partner, Int32 cnnTimeOut)
        {
            try
            {
                cnnString = String.Format("User ID={0};" +
                                          "Password={1};" +
                                          "Data Source={2};" +
                                          "Initial Catalog={3};" +
                                          "Failover Partner={4};" +
                                          "Connection Timeout={5};" +
                                          "Persist Security Info=True;",
                                          user,
                                          this.CrypTo(pwd, ik.FunctionsDB.enEncryption.Descriptar),
                                          dataSource,
                                          catalog,
                                          partner,
                                          cnnTimeOut.ToString());
                if (cnnDB == null)
                    cnnDB = new SqlConnection(cnnString);
                if (cnnDB.State != ConnectionState.Open)
                    cnnDB.Open();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region "GetSpidCnn"
        /// <summary>
        /// retorna o número da conexão (SPID)
        /// </summary>
        /// <returns>Int32</returns>
        public Int32 GetSpidCnn()
        {
            try
            {
                return Convert.ToInt32(base.ExecuteScalar("select @@spid"));
            }
            catch (Exception)
            {
                
                throw;
            }
        }
        #endregion

        #region "BulkCopy"
        /// <summary>
        /// copia de datatable para uma tabela
        /// </summary>
        /// <param name="dt">DataTable origem</param>
        /// <param name="table">tabela de destino</param>
        public void BulkCopy(DataTable dt, String table, SqlBulkCopyOptions option)
        {
            try
            {
                using (SqlBulkCopy _bcopy = new SqlBulkCopy(cnnString, option))
                {
                    _bcopy.DestinationTableName = table;
                    _bcopy.BulkCopyTimeout = 0;
                    _bcopy.WriteToServer(dt);
                    _bcopy.Close();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
    }

}

